home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11659 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: apccorp.apcc.com!root
  2. From: nfegan@apcc.com (Noel Fegan)
  3. Newsgroups: comp.lang.c++,comp.os.linux.development.apps,comp.os.linux.misc
  4. Subject: Re: Are str* functions okay in C++?
  5. Date: Fri, 15 Mar 1996 16:35:59 GMT
  6. Organization: American Power Conversion
  7. Message-ID: <4ic6ba$mhe@apccorp.apcc.com>
  8. References: <4hpvi0$gt6@news.platinum.com> <Do54G9.4Ly.0.server@indra.com>
  9. NNTP-Posting-Host: hewie.galway.apcc.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. bear@ (Bear Giles) wrote:
  13.  
  14.  
  15. >   <snip>
  16. >void procedure (argument...)
  17. >   {
  18. >   static char * buffer = 0;
  19.  
  20. >   if (buffer == 0)
  21. >       buffer = malloc (2048);  /* or any _large_ number */
  22.  
  23. >   ...
  24. >   sprintf (buffer, format,...)
  25. >   path = strdup (buffer);
  26. >   ...
  27. >   <snip>
  28.  
  29. In my experience The use of malloc and free in a C++ application should be
  30. avoided. From what I've read this is generally what is advised. This would also
  31. mean that functions like strdup should be avoided also, because, as you
  32. mentioned, you have to use free on the memory block returned by strdup, not
  33. delete. I'm sure you are very careful with this but others maintaining you code
  34. may not realise the implications.
  35.  
  36. I'm not saying that they do not work but it introduce the potential problem
  37. where free is using on a "new"ed memory block, etc. 
  38.  
  39. Apart from this, I do not follow your reason for using a "captive memory leak".
  40. This memory block is not deleted at any stage during the program, and as such is
  41. a memory leak as you mentioned. You rely on the good nature of the operating
  42. system to clean up all the memory taken by the program when it has finished
  43. executing, even if the program does not delete it itself. I'm think this is a
  44. bit risky. If you main concern is fragementation of memory or stack size why not
  45. have the following:
  46.  
  47. void procedure (argument...)
  48.   {
  49.   static char buffer[2048];
  50.  
  51.   ...
  52.   sprintf(buffer, format,...);
  53.   path = new char[strlen(buffer)+1];
  54.   strcpy(path, buffer);
  55.   ...
  56.  
  57. This would be a static buffer which is allocated by the program when it is
  58. initializing and free when the program terminates, there is no memory leak and
  59. the memory does not get fragmented.
  60.  
  61. I would recommend using a string class instead of having to add lines like "path
  62. = new ..." and the "strcpy(..." every time you want to copy a string. Or you
  63. could write a function which did the equivalent of strdup but used new instead
  64. of malloc.
  65. --
  66. Noel Fegan
  67. European Software Development Department
  68. American Power Conversion
  69. I don't speak for APC...
  70. nfegan@apcc.com
  71.  
  72.